package com.cndw.kungfu.model.general;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.cndw.commons.util.BlankUtils;
import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.config.ConstantsIntKey;
import com.cndw.kungfu.dao.RankingListDao;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.domain.Title;
import com.cndw.kungfu.domain.Union;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.DataSerialize;
import com.cndw.kungfu.model.disciple.PlayerMartial;
import com.cndw.kungfu.model.disciple.domain.Disciple;
import com.cndw.kungfu.vo.PassThroughRecordVO;
import com.cndw.kungfu.vo.rankingvo.OtherRankingVO;
import com.cndw.kungfu.vo.rankingvo.PlayerRankingVO;
import com.cndw.kungfu.vo.rankingvo.UnionRankingVO;

@Service
public class RankingListManager {

	@Resource
	private RankingListDao rankingListDao;

	private static volatile int nextUpdateTime = 0;

	/**
	 * 等级排行版
	 */
	private static final Map<Long, OtherRankingVO> levelRankings = new HashMap<Long, OtherRankingVO>();

	/**
	 * 战斗力排行版
	 */
	private static final Map<Long, OtherRankingVO> attackRankings = new HashMap<Long, OtherRankingVO>();

	/**
	 * 徒弟排行版
	 */
	private static final Map<Long, OtherRankingVO> discipleRankings = new HashMap<Long, OtherRankingVO>();

	/**
	 * 帮派排行版
	 */
	private static final Map<Integer, UnionRankingVO> unionRankings = new HashMap<Integer, UnionRankingVO>();

	/**
	 * 徒弟排行版
	 */
	private static final Map<Long, OtherRankingVO> collectRankings = new HashMap<Long, OtherRankingVO>();

	/**
	 * 个人排名
	 */
	private static final Map<Long, PlayerRankingVO> playerRankings = new HashMap<Long, PlayerRankingVO>();

	/**
	 * 神兵阵排行
	 */
	private static final Map<Long, PassThroughRecordVO> towerRankings = new HashMap<Long, PassThroughRecordVO>();

	/**
	 * 排行版更新时间
	 */
	private static final Map<String, Integer> rankingUpdateTime = new HashMap<String, Integer>();

	/**
	 * 排行版类型
	 */
	private static final String[] RANKING_TYPE = { "level", "attack", "disciple", "union", "collect", "tower" };

	public void changePlayerName(Long playerId, String PlayerName) {
		if (levelRankings.containsKey(playerId)) {
			levelRankings.get(playerId).setPlayerName(PlayerName);
		}

		if (attackRankings.containsKey(playerId)) {
			attackRankings.get(playerId).setPlayerName(PlayerName);
		}

		if (discipleRankings.containsKey(playerId)) {
			discipleRankings.get(playerId).setPlayerName(PlayerName);
		}

		if (collectRankings.containsKey(playerId)) {
			collectRankings.get(playerId).setPlayerName(PlayerName);
		}

		if (towerRankings.containsKey(playerId)) {
			towerRankings.get(playerId).setName(PlayerName);
		}
	}

	/**
	 * 获取个人排名
	 * 
	 * @param playerId
	 * @return
	 */
	public PlayerRankingVO getPlayerRanking(long playerId) {
		if (!playerRankings.containsKey(playerId) || checkUpdate(playerRankings.get(playerId))) {

			if (!playerRankings.containsKey(playerId)) {
				PlayerRankingVO playerRanking = rankingListDao.getPlayerRanking(playerId);
				if (BlankUtils.isBlank(playerRanking)) {
					playerRanking = new PlayerRankingVO(playerId);
					rankingListDao.addPlayerRanking(playerRanking);
				}
				checkUpdate(playerRanking);// 如果不存在，设置下次更新时间
				playerRankings.put(playerId, playerRanking);
			}

			PlayerRankingVO playerRanking = playerRankings.get(playerId);
			Player player = Context.getPlayerServer().getByPlayerId(playerId);

			setDiscipleRanking(playerRanking); // 徒弟
			setUnionRanking(playerRanking); // 帮派
			setLevelRanking(player, playerRanking);// 等级
			setAttackRanking(player, playerRanking); // 战斗力
			setCollectRanking(player, playerRanking);// 图鉴
			setTowerRanking(player, playerRanking);// 千层塔

			// 更新缓存和数据库
			rankingListDao.updatePlayerRanking(playerRanking);
		}

		return playerRankings.get(playerId);
	}

	/**
	 * 更新个人等级排名
	 * 
	 * @return
	 */
	public void setLevelRanking(Player player, PlayerRankingVO playerRanking) {

		// 最高等级者
		List<OtherRankingVO> levelRankingVOs = getLevelRanking();
		if (!BlankUtils.isBlank(levelRankingVOs)) {
			OtherRankingVO maxRanking = levelRankingVOs.get(0);
			playerRanking.setMaxLevel(maxRanking.getValue());
			playerRanking.setMaxLevelName(maxRanking.getPlayerName());
		}

		OtherRankingVO levelRankingVO = levelRankings.get(playerRanking.getPlayerId());
		if (BlankUtils.isBlank(levelRankingVO)) { // 100名之外
			playerRanking.setLevel(player.getLevel());
			playerRanking.setLevelChange(playerRanking.getLevelRank() - 101);
			playerRanking.setLevelRank(101);
		} else { // 100名之内
			playerRanking.setLevel(levelRankingVO.getValue());
			playerRanking.setLevelChange(playerRanking.getLevelRank() - levelRankingVO.getRanking());
			playerRanking.setLevelRank(levelRankingVO.getRanking());
		}
	}

	/**
	 * 获取等级排名
	 * 
	 * @return
	 */
	public List<OtherRankingVO> getLevelRanking() {
		if (checkUpdate(RANKING_TYPE[0]) || levelRankings.isEmpty()) {
			levelRankings.clear();
			List<Player> players = rankingListDao.getLevelRanking();
			Map<Integer, Long> newRanking = new HashMap<Integer, Long>();
			Map<Long, OtherRankingVO> tmpRank = new HashMap<Long, OtherRankingVO>();
			int size = Context.getCacheCenter().getTitles(Constants.titleLevelRanking).size();
			for (int i = 0, j = players.size(); i < j; i++) {
				Player player = players.get(i);
				OtherRankingVO levelRankingVO = new OtherRankingVO();
				levelRankingVO.setRanking(i + 1);
				levelRankingVO.setPlayerId(player.getPlayerId());
				levelRankingVO.setPlayerLevel(player.getLevel());
				levelRankingVO.setPlayerName(player.getPlayerName());
				levelRankingVO.setVocationId(player.getVocationId());
				levelRankingVO.setValue(player.getLevel());
				tmpRank.put(player.getPlayerId(), levelRankingVO);
				if (i < size) {
					newRanking.put(i + 1, player.getPlayerId());
				}
			}
			levelRankings.putAll(tmpRank);
			
			changeRanking(Constants.titleLevelRanking, newRanking);
		}

		List<OtherRankingVO> ranking = new ArrayList<OtherRankingVO>(levelRankings.values());
		Collections.sort(ranking);
		return ranking;
	}

	public int getWorldLevel() {
		List<OtherRankingVO> levelOrders = getLevelRanking();
		if (levelOrders == null || levelOrders.size() == 0) {
			LogUtil.getMain().error("world level init err ");
			return 0;
		}
		int k = 0, m = 0;
		for (OtherRankingVO e : levelOrders) {
			if (e.getRanking() <= 10 && e.getRanking() > 0) {
				m += e.getPlayerLevel();
				k++;
			}
		}
		return m / k;
	}

	/**
	 * 更新个人战斗力排名
	 * 
	 * @return
	 */
	public void setAttackRanking(Player player, PlayerRankingVO playerRanking) {

		// 最高战斗力者
		List<OtherRankingVO> attackRankingVOs = getAttackRanking();
		if (!BlankUtils.isBlank(attackRankingVOs)) {
			OtherRankingVO maxRanking = attackRankingVOs.get(0);
			playerRanking.setMaxAttack(maxRanking.getValue());
			playerRanking.setMaxAttackName(maxRanking.getPlayerName());
		}

		OtherRankingVO attackRankingVO = attackRankings.get(playerRanking.getPlayerId());
		if (BlankUtils.isBlank(attackRankingVO)) {
			playerRanking.setAttack(player.getScore());
			playerRanking.setAttackChange(playerRanking.getAttackRank() - 101);
			playerRanking.setAttackRank(101);
		} else {
			playerRanking.setAttack(attackRankingVO.getValue());
			playerRanking.setAttackChange(playerRanking.getAttackRank() - attackRankingVO.getRanking());
			playerRanking.setAttackRank(attackRankingVO.getRanking());
		}
	}

	/**
	 * 获取战斗力排名
	 * 
	 * @return
	 */
	public List<OtherRankingVO> getAttackRanking() {
		if (checkUpdate(RANKING_TYPE[1])) {
			attackRankings.clear();
			List<Player> players = rankingListDao.getAttackRanking();
			Map<Integer, Long> newRanking = new HashMap<Integer, Long>();
			int size = Context.getCacheCenter().getTitles(Constants.titleAttackRanking).size();
			for (int i = 0, j = players.size(); i < j; i++) {
				Player player = players.get(i);
				OtherRankingVO attackRankingVO = new OtherRankingVO();
				attackRankingVO.setRanking(i + 1);
				attackRankingVO.setPlayerId(player.getPlayerId());
				attackRankingVO.setPlayerLevel(player.getLevel());
				attackRankingVO.setPlayerName(player.getPlayerName());
				attackRankingVO.setVocationId(player.getVocationId());
				attackRankingVO.setValue(player.getScore());
				attackRankings.put(player.getPlayerId(), attackRankingVO);
				if (i < size) {
					newRanking.put(i + 1, player.getPlayerId());
				}
			}

			changeRanking(Constants.titleAttackRanking, newRanking);
		}
		List<OtherRankingVO> ranking = new ArrayList<OtherRankingVO>(attackRankings.values());
		Collections.sort(ranking);
		return ranking;
	}

	/**
	 * 更新个人徒弟排名
	 * 
	 * @return
	 */
	public void setDiscipleRanking(PlayerRankingVO playerRanking) {

		// 最高徒弟评分者
		List<OtherRankingVO> discipleRankingVOs = getDiscipleRanking();
		if (!BlankUtils.isBlank(discipleRankingVOs)) {
			OtherRankingVO maxRanking = discipleRankingVOs.get(0);
			playerRanking.setMaxDisciple(maxRanking.getValue());
			playerRanking.setMaxDiscipleName(maxRanking.getPlayerName());
		}

		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(playerRanking.getPlayerId());
		if (!BlankUtils.isBlank(playerMartial) && !BlankUtils.isBlank(playerMartial.getDisciples())) {
			OtherRankingVO discipleRankingVO = discipleRankings.get(playerRanking.getPlayerId());
			if (BlankUtils.isBlank(discipleRankingVO)) { // 100名之外
				playerRanking.setDisciple(playerMartial.getScore());
				playerRanking.setDiscipleChange(playerRanking.getDiscipleRank() - 101);
				playerRanking.setDiscipleRank(101);
			} else { // 100名之内
				playerRanking.setDisciple(discipleRankingVO.getValue());
				playerRanking.setDiscipleChange(playerRanking.getDiscipleRank() - discipleRankingVO.getRanking());
				playerRanking.setDiscipleRank(discipleRankingVO.getRanking());
			}
		} else { // 没有徒弟
			playerRanking.setDisciple(0);
			playerRanking.setDiscipleChange(0 - playerRanking.getDiscipleRank());
			playerRanking.setDiscipleRank(0);
		}
	}

	/**
	 * 获取徒弟排行
	 * 
	 * @return
	 */
	public List<OtherRankingVO> getDiscipleRanking() {
		if (checkUpdate(RANKING_TYPE[2])) {
			discipleRankings.clear();
			List<Disciple> disciples = rankingListDao.getDiscipleRanking();
			Map<Integer, Long> newRanking = new HashMap<Integer, Long>();
			int size = Context.getCacheCenter().getTitles(Constants.titleDiscipleRanking).size();
			for (int i = 0, j = disciples.size(); i < j; i++) {
				Disciple disciple = disciples.get(i);
				Player player = Context.getPlayerServer().getByPlayerId(disciple.getPlayerId());
				if (player == null) {
					LogUtil.getMain().error("no player " + disciple.getPlayerId());
					continue;
				}
				OtherRankingVO discipleRankingVO = new OtherRankingVO();
				discipleRankingVO.setRanking(i + 1);
				discipleRankingVO.setValue(disciple.getScore());
				discipleRankingVO.setPlayerId(player.getPlayerId());
				discipleRankingVO.setVocationId(player.getVocationId());
				discipleRankingVO.setPlayerLevel(player.getLevel());
				discipleRankingVO.setPlayerName(player.getPlayerName());
				discipleRankings.put(player.getPlayerId(), discipleRankingVO);
				if (i < size) {
					newRanking.put(i + 1, player.getPlayerId());
				}
			}

			changeRanking(Constants.titleDiscipleRanking, newRanking);
		}

		List<OtherRankingVO> ranking = new ArrayList<OtherRankingVO>(discipleRankings.values());
		Collections.sort(ranking);
		return ranking;
	}

	/**
	 * 更新个人帮派排名
	 * 
	 * @return
	 */
	public void setUnionRanking(PlayerRankingVO playerRanking) {

		// 最高等级帮派
		List<UnionRankingVO> unionRankingVOs = getUnionRanking();
		if (!BlankUtils.isBlank(unionRankingVOs)) {
			UnionRankingVO maxRanking = unionRankingVOs.get(0);
			playerRanking.setMaxUnion(maxRanking.getLevel());
			playerRanking.setMaxUnionName(maxRanking.getName());
		}

		Union union = Context.getUnionServer().getUnionByPlayerId(playerRanking.getPlayerId());
		if (!BlankUtils.isBlank(union)) {
			UnionRankingVO unionRankingVO = unionRankings.get(union.getId());
			if (BlankUtils.isBlank(unionRankingVO)) { // 100名之外
				playerRanking.setUnions(union.getLevel());
				playerRanking.setUnionChange(playerRanking.getUnionRank() - 101);
				playerRanking.setUnionRank(101);
			} else { // 100名之内
				playerRanking.setUnions(unionRankingVO.getLevel());
				playerRanking.setUnionChange(playerRanking.getUnionRank() - unionRankingVO.getRanking());
				playerRanking.setUnionRank(unionRankingVO.getRanking());
			}
		} else {
			// 帮派如解散需要更新之前的排名
			playerRanking.setUnions(0);
			playerRanking.setUnionChange(0 - playerRanking.getUnionRank());
			playerRanking.setUnionRank(0);
		}
	}

	/**
	 * 获取帮派排名
	 * 
	 * @return
	 */
	public List<UnionRankingVO> getUnionRanking() {
		if (checkUpdate(RANKING_TYPE[3])) {
			unionRankings.clear();
			List<Union> unions = rankingListDao.getUnionRanking();
			Map<Integer, Long> unionIds = new HashMap<Integer, Long>();
			int size = Context.getCacheCenter().getTitles(Constants.titleUnionRanking).size();
			for (int i = 0, j = unions.size(); i < j; i++) {
				Union union = unions.get(i);
				UnionRankingVO unionRankingVO = new UnionRankingVO();
				unionRankingVO.setRanking(i + 1);
				unionRankingVO.setUnionId(union.getId());
				unionRankingVO.setName(union.getName());
				unionRankingVO.setLevel(union.getLevel());
				unionRankingVO.setAsset(union.getAsset());
				unionRankingVO.setResource(union.getResource());
				unionRankingVO.setMemberCount(union.getMemberCount());
				unionRankings.put(union.getId(), unionRankingVO);
				if (i < size) {
					unionIds.put(i + 1, (long) union.getId());
				}
			}

			changeUnionRanking(unionIds);
		}

		List<UnionRankingVO> ranking = new ArrayList<UnionRankingVO>(unionRankings.values());
		Collections.sort(ranking);
		return ranking;
	}

	/**
	 * 更新个人图鉴排名
	 * 
	 * @return
	 */
	public void setCollectRanking(Player player, PlayerRankingVO playerRanking) {
		List<OtherRankingVO> collectRankingVOs = getCollectRanking();
		if (!BlankUtils.isBlank(collectRankingVOs)) {
			OtherRankingVO maxRanking = collectRankingVOs.get(0);
			playerRanking.setMaxCollect(maxRanking.getValue());
			playerRanking.setMaxCollectName(maxRanking.getPlayerName());
		}

		OtherRankingVO collectRankingVO = collectRankings.get(playerRanking.getPlayerId());
		if (BlankUtils.isBlank(collectRankingVO)) {
			if (collectRankings.size() > 100) { // 100名外
				playerRanking.setCollect(player.getPoints());
				playerRanking.setCollectChange(playerRanking.getCollectRank() - 101);
				playerRanking.setCollectRank(101);
			} else { // 根本就没有成就点
				playerRanking.setCollect(0);
				playerRanking.setCollectChange(0 - playerRanking.getCollectRank());
				playerRanking.setCollectRank(0);
			}
		} else {// 100名内
			playerRanking.setCollect(collectRankingVO.getValue());
			playerRanking.setCollectChange(playerRanking.getCollectRank() - collectRankingVO.getRanking());
			playerRanking.setCollectRank(collectRankingVO.getRanking());
		}
	}

	/**
	 * 获取图鉴排名
	 * 
	 * @return
	 */
	public List<OtherRankingVO> getCollectRanking() {
		if (checkUpdate(RANKING_TYPE[4])) {
			collectRankings.clear();
			List<Player> players = rankingListDao.getCollectRanking();
			// Map<Integer, Long> newRanking = new HashMap<Integer, Long>();
			// int size =
			// Context.getCacheCenter().getTitles(Constants.titleCollectRanking).size();
			for (int i = 0, j = players.size(); i < j; i++) {
				Player player = players.get(i);
				OtherRankingVO collectRankingVO = new OtherRankingVO();
				collectRankingVO.setRanking(i + 1);
				collectRankingVO.setPlayerId(player.getPlayerId());
				collectRankingVO.setPlayerLevel(player.getLevel());
				collectRankingVO.setPlayerName(player.getPlayerName());
				collectRankingVO.setVocationId(player.getVocationId());
				collectRankingVO.setValue(player.getPoints());
				collectRankings.put(player.getPlayerId(), collectRankingVO);
				/*
				 * if(i < size){ newRanking.put(i + 1, player.getPlayerId()); }
				 */
			}
			// 屏蔽宝鉴排行称号
			// changeRanking(Constants.titleCollectRanking, newRanking);
		}

		List<OtherRankingVO> ranking = new ArrayList<OtherRankingVO>(collectRankings.values());
		Collections.sort(ranking);
		return ranking;
	}

	/**
	 * 设置千层塔排行
	 */
	public void setTowerRanking(Player player, PlayerRankingVO playerRanking) {
		List<PassThroughRecordVO> scpRanking = getTowerRanking();
		if (!BlankUtils.isBlank(scpRanking)) {
			PassThroughRecordVO maxRanking = scpRanking.get(0);
			playerRanking.setMaxTower(maxRanking.getSortLevel());
			playerRanking.setMaxTowerName(maxRanking.getName());
		}

		PassThroughRecordVO ptrVo = towerRankings.get(playerRanking.getPlayerId());
		if (BlankUtils.isBlank(ptrVo)) {
			if (towerRankings.size() > 100) {
				int sortLevel = player.getDataInfo().intDataGet(ConstantsIntKey.soldierCpPass);
				int ranking = 101;
				sortLevel = sortLevel > 1 ? sortLevel - 1 : 0;
				playerRanking.setTower(sortLevel);
				playerRanking.setTowerChange(playerRanking.getTowerRank() - ranking);
				playerRanking.setTowerRank(ranking);
			} else {
				playerRanking.setTower(0);
				playerRanking.setTowerChange(0);
				playerRanking.setTowerRank(0);
			}
		} else {
			playerRanking.setTower(ptrVo.getSortLevel());
			playerRanking.setTowerChange(playerRanking.getTowerRank() - ptrVo.getRanking());
			playerRanking.setTowerRank(ptrVo.getRanking());
		}
	}

	/**
	 * 获取千层塔排名
	 * 
	 * @return
	 */
	public List<PassThroughRecordVO> getTowerRanking() {
		if (checkUpdate(RANKING_TYPE[5])) {
			towerRankings.clear();
			Collection<PassThroughRecordVO> scpRanking = Context.getUndercityServer().getSCPRanking();
			for (PassThroughRecordVO ptr : scpRanking) {
				PassThroughRecordVO e = new PassThroughRecordVO();
				e.setLevel(ptr.getLevel());
				e.setName(ptr.getName());
				e.setPlayerId(ptr.getPlayerId());
				e.setRanking(ptr.getRanking());
				e.setSortLevel(ptr.getSortLevel());
				e.setSpendTime(ptr.getSpendTime());
				e.setVocationId(ptr.getVocationId());
				e.setTime(ptr.getTime());
				towerRankings.put(ptr.getPlayerId(), e);
			}

			List<PassThroughRecordVO> tempRanking = new ArrayList<PassThroughRecordVO>(towerRankings.values());
			Collections.sort(tempRanking);
			Map<Integer, Long> newRanking = new HashMap<Integer, Long>();
			int size = Context.getCacheCenter().getTitles(Constants.titleSCpRanking).size();
			for (int i = 1; i <= tempRanking.size() && i <= size; i++) {
				newRanking.put(i, tempRanking.get(i - 1).getPlayerId());
			}
			changeRanking(Constants.titleSCpRanking, newRanking);
		}

		List<PassThroughRecordVO> ranking = new ArrayList<PassThroughRecordVO>(towerRankings.values());
		Collections.sort(ranking);
		return ranking;
	}

	/**
	 * 获取下次更新的时间
	 * 
	 * @return
	 */
	public int getNextUpdateTime() {
		return nextUpdateTime;
	}

	/**
	 * 检查是否到达更新时间，true表示到达更新时间
	 * 
	 * @return
	 */
	private synchronized boolean checkUpdate(String rankingType) {

		if (rankingUpdateTime.containsKey(rankingType)) {
			if (rankingUpdateTime.get(rankingType).intValue() < TimeProcesser.getUnixTime()) {
				for (int i = TimeProcesser.getMorningTime(); true; i += 7200) { // 2个小时更新一次
					if (TimeProcesser.getUnixTime() < i) {
						nextUpdateTime = i;
						rankingUpdateTime.put(rankingType, nextUpdateTime);
						return true;
					}
				}
			} else {
				return false;
			}
		} else {
			for (int i = TimeProcesser.getMorningTime(); true; i += 7200) { // 2个小时更新一次
				if (TimeProcesser.getUnixTime() < i) {
					nextUpdateTime = i;
					rankingUpdateTime.put(rankingType, nextUpdateTime);
					return true;
				}
			}
		}
	}

	public void clearUpdateTime(int type) {
		rankingUpdateTime.remove(RANKING_TYPE[type]);
	}

	/**
	 * 检查是否达到更新时间，用户个人排行检查
	 * 
	 * @param playerRankingVO
	 * @return
	 */
	public synchronized boolean checkUpdate(PlayerRankingVO playerRankingVO) {
		if (playerRankingVO.getNextUpdateTime() < TimeProcesser.getUnixTime()) {
			for (int i = TimeProcesser.getMorningTime(); true; i += 7200) { // 2个小时更新一次
				if (TimeProcesser.getUnixTime() < i) {
					nextUpdateTime = i;
					playerRankingVO.setNextUpdateTime(nextUpdateTime);
					return true;
				}
			}
		} else {
			return false;
		}
	}

	/**
	 * 移除帮派称号
	 */
	private void removeUnionTitle(int unionId, Title title) {
		Union union = Context.getUnionServer().getUnionById(unionId);
		if (union != null) {
			// for(long playerId : union.getMemberIds()){
			long playerId = union.getPlayerId();
			Context.getGeneralServer().removeTitle(playerId, title.getId());
			// }
		}
	}

	/**
	 * 增加帮派称号
	 */
	private void addUnionTitle(int unionId, Title title) {
		Union union = Context.getUnionServer().getUnionById(unionId);
		if (union != null) {
			// for(long playerId : union.getMemberIds()){
			long playerId = union.getPlayerId();
			Context.getGeneralServer().addTitle(playerId, title.getId());
			// }
		}
	}

	/**
	 * 更改帮派称号
	 */
	private void changeUnionRanking(Map<Integer, Long> newUnionRanking) {
		Map<Integer, Long> oldUnionRanking = DataSerialize.getInstance().getRanking(Constants.titleUnionRanking);
		int oldId = 0, newId = 0;
		int oldSize = oldUnionRanking == null ? 0 : oldUnionRanking.size(), newSize = newUnionRanking == null ? 0 : newUnionRanking.size();
		for (int i = 1; i <= Math.max(oldSize, newSize); i++) {
			if (oldUnionRanking != null && oldUnionRanking.containsKey(i)) {
				oldId = oldUnionRanking.get(i).intValue();
			}
			if (newUnionRanking != null && newUnionRanking.containsKey(i)) {
				newId = newUnionRanking.get(i).intValue();
			}
			Title title = Context.getCacheCenter().getTitle(Constants.titleUnionRanking, i);
			if (oldId != newId) {
				if (oldId != 0) {
					removeUnionTitle(oldId, title);
				}
				if (newId != 0) {
					addUnionTitle(newId, title);
				}
			} else {
				// addUnionTitle(newId, title);
			}
		}

		DataSerialize.getInstance().changeRanking(Constants.titleUnionRanking, newUnionRanking);
		DataSerialize.getInstance().commitSync();
	}

	/**
	 * 更改称号
	 */
	private void changeRanking(int titleType, Map<Integer, Long> newRanking) {
		Map<Integer, Long> oldRanking = DataSerialize.getInstance().getRanking(titleType);
		long oldId = 0, newId = 0;
		int oldSize = oldRanking == null ? 0 : oldRanking.size(), newSize = newRanking == null ? 0 : newRanking.size();
		for (int i = 1; i <= Math.max(oldSize, newSize); i++) {
			if (oldRanking != null && oldRanking.containsKey(i)) {
				oldId = oldRanking.get(i);
			}
			if (newRanking != null && newRanking.containsKey(i)) {
				newId = newRanking.get(i);
			}

			Title title = Context.getCacheCenter().getTitle(titleType, i);

			if (oldId != newId) {
				if (oldId != 0) {
					Context.getGeneralServer().removeTitle(oldId, title.getId());
				}
				if (newId != 0) {
					Context.getGeneralServer().addTitle(newId, title.getId());
				}
			} else {
				// Context.getGeneralServer().addTitle(newId, title.getId(),
				// false);
			}
		}

		DataSerialize.getInstance().changeRanking(titleType, newRanking);
		DataSerialize.getInstance().commitSync();
	}
}
